package in.muton.edushare.components.autocomplete.api;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.components.calendar.api.UniqueElementList;
import in.muton.edushare.wicket.models.CayenneList;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.model.IModel;

import com.edushare.common.dao.BatchDAO;
import com.edushare.common.dao.CurriculumDAO;
import com.edushare.common.dao.UniversitiesDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.Batches;
import com.edushare.common.vo.Curricula;
import com.edushare.common.vo.GenericAudience;
import com.edushare.common.vo.Poll;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class BatchAudienceHelper extends NewAudienceHelper {
	private static final long serialVersionUID = 3939655678622266521L;

	public BatchAudienceHelper(ORMInterface orm) {
		List<Universities> univList = new UniversitiesDAO(orm)
				.getAllUniversities();
		List<Curricula> cList = new ArrayList<Curricula>();
		for (Universities univ : univList) {
			cList.addAll(new CurriculumDAO(orm).getCurricula(univ));
		}
		for (Curricula c : cList) {
			batchList.add(c.getBatches());
		}
	}

	public List<String> getStringList() {
		List<String> list = new ArrayList<String>();
		List<IModel<Batches>> temp = batchList.getObject();
		for (IModel<Batches> t : temp) {
			list.add("["+t.getObject().getTitle()+"];");
		}
		return list;
	}

	public IModel<Batches> getBatch(String s) {
		List<IModel<Batches>> ci = batchList.getObject();
		for (IModel<Batches> c : ci) {
			if (s.contains(c.getObject().getTitle()) && s.contains("[")
					&& s.contains("]"))
				return c;
		}
		return null;
	}

	public CayenneList<Users> getAllUsers(String s) {
		IModel<Batches> ci = getBatch(s);
		if (ci != null)
			return new CayenneList<Users>(new BatchDAO(EduShareSession.get()
					.getSessionORMInterface()).getUsers(ci.getObject()));
		return new CayenneList<Users>(null);
	}

	public CayenneList<GenericAudience> getAudiences(String s) {
		return null;
	}

	private UniqueElementList<Batches> batchList = new UniqueElementList<Batches>(
			null);

	@Override
	public String getNameForAudience(String s) {
		return getBatch(s).getObject().getTitle();
	}

	@Override
	public int getRoleId(String s) {
		return Poll.POLL_STUDENTS;
	}
}
